package com.woniu.cms.lease.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.cms.lease.core.entity.LeaseStorage;
import com.woniu.cms.lease.core.mapper.LeaseStorageMapper;
import com.woniu.cms.lease.core.VO.StorageVO;
import com.woniu.cms.lease.core.entity.LeaseManage;
import com.woniu.cms.lease.core.mapper.LeaseManageMapper;
import com.woniu.cms.lease.core.service.impl.LeaseManageServiceImpl;
import com.woniu.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author romantic sheep
 * @since 2022-09-18
 */
@RestController
@RequestMapping("/lease-manage")
@Api(tags = "仪表管理")
public class LeaseManageController {
    @Autowired
    private LeaseManageServiceImpl leaseManageService;
    @Autowired
    private LeaseManageMapper leaseManageMapper;
    @Autowired
    private LeaseStorageMapper leaseStorageMapper;


    @GetMapping("/queryAll")
    @ApiOperation("查询")
    public List<LeaseManage> queryAll() {
        return leaseManageService.queryAll();
    }

    @PostMapping("/apply")
    @ApiOperation("新增仪表申请单")
    public Result apply(@RequestBody LeaseManage leaseManage) {
        int add = leaseManageService.add(leaseManage);
        return Result.common(add);
    }

    @GetMapping("/Alll")
    @ApiOperation("返回vo")
    public List<StorageVO> AlLl() {
        List<StorageVO> alll = leaseManageService.alll();
        return alll;
    }
    @PutMapping("/modify")
    @ApiOperation("修改仪表申请单")
    public Result modify(@RequestBody LeaseManage leaseManage){

        boolean modify = leaseManageService.modify(leaseManage);
        Result<Boolean> common = Result.common(modify);
        return common;
    }


    @PutMapping("{id}")
    @ApiOperation("归还仪器")
    public Result up(@PathVariable Integer id){
        LeaseManage byId = leaseManageService.getById(id);
        Integer storageId = byId.getStorageId();
        List<LeaseStorage> all = leaseStorageMapper.all();
        for (LeaseStorage e : all) {
            boolean b1 = e.getId().equals(storageId);

            if (b1){
                leaseStorageMapper.Revise(e.getId());
                leaseManageMapper.returne(id);
            }
        }
        return Result.ok();
    }

    @PutMapping("/Totalrent/{id}")
    @ApiOperation("生成使用时间,租金合计")
    public Result Totalrent(@PathVariable Integer id){
        Integer integer = leaseManageService.TotalRent(id);
        return Result.common(integer);
    }

    @GetMapping("/cost")
    @ApiOperation("仪表租赁费用接口")
    public Result cost() {
        QueryWrapper<LeaseManage> se = new QueryWrapper<>();
        QueryWrapper<LeaseManage> total = se.select("total_rent");
        List<LeaseManage> settlementExpenses = leaseManageMapper.selectList(total);
        return Result.common(settlementExpenses);
    }
}



